package com.example.electricbicycle.controller;

import utils.DBUtils;
import com.example.electricbicycle.po.User;
import com.example.electricbicycle.service.UserService;
import com.example.electricbicycle.service.UserServiceImpl;
import com.example.electricbicycle.service.CustomerService;
import com.example.electricbicycle.service.CustomerServiceImpl;
import com.example.electricbicycle.po.Customer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private final UserService userService = new UserServiceImpl();
    private final CustomerService customerService = new CustomerServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out=response.getWriter();
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = userService.login(username, password);

        if (user != null) {
            HttpSession session = request.getSession();
            session.setAttribute("currentUser", user);

                        // 根据角色重定向
                        switch (user.getRole()) {
                            case 4: // 系统管理员
                                // 1. 获取所有客户数据
                                List<Customer> allCustomers = customerService.getAllCustomers(1, 10000);

                                // 2. 获取所有用户数据
                                List<User> allUsers = userService.getAllUsers(1, 10000);

                                // 3. 设置到request属性中
                                request.setAttribute("allCustomers", allCustomers);
                                request.setAttribute("allUsers", allUsers);

                                // 4. 转发到管理首页
                                request.getRequestDispatcher("/admin/list.jsp").forward(request, response);
                                break;
                            case 0:
                                response.sendRedirect(request.getContextPath() + "/purchase/list");
                                break;
                            case 1:
                                response.sendRedirect(request.getContextPath() + "/warehouse/warehouse_admin.jsp");
                                break;
                            case 2:
                                response.sendRedirect(request.getContextPath() + "/salesAdmin?action=list");
                                break;//销售管理员

                            case 3:
                                response.sendRedirect(request.getContextPath() + "/afterSalesAdmin?action=list");
                                break;
                            default:
                                response.sendRedirect(request.getContextPath() + "/login.jsp?error=2");
                        }
                    } else {
                        // 用户不存在或密码错误
                            request.setAttribute("errorMsg", "用户名或密码错误");
                            request.getRequestDispatcher("login.jsp").forward(request, response);
                                    }
                }
            }